package com.app.webservice;

import java.util.ArrayList;
import java.util.List;

import com.app.entidades.Musicas;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class MusicaRest {

	private static final String URLWS = "http://lucianokikao.com.br/mobile/";
	
	public Musicas getMusica(int id) throws Exception{
		String[] resposta = new WebServiceApp().get(URLWS+id);
		
		if(resposta[0].equals("200")){
			Gson gson = new Gson();
			Musicas ingrediente = gson.fromJson(resposta[1], Musicas.class);
			return ingrediente;
		}else {
	         throw new Exception(resposta[1]);
	     }	
		
	}
	
	 public List<Musicas> getListaMusicas() throws Exception {
		 String url ="http://lucianokikao.com.br/mobile/compo.php?user=q1w2e3r4t5&pass=t5r4e3w2q1";
	     String[] resposta = new WebServiceApp().get(url);
	    
	     if (resposta[0].equals("200")) {
	         Gson gson = new Gson();
	         List<Musicas> listaMusica = new ArrayList<Musicas>();
	         JsonParser parser = new JsonParser();
	         
	         try{
	        	 JsonElement element = parser.parse(resposta[1]);
	        	 JsonArray array = element.getAsJsonArray();
	        	 for (int i = 0; i < array.size(); i++) {
	 	            listaMusica.add(gson.fromJson(array.get(i), Musicas.class));
	 	         }
	         }catch(Exception e){
	        	 throw new Exception("Falha ao acessar Internet");
	         }
	         
	        return listaMusica;
	     }else {
	         throw new Exception(resposta[1]);
	     }
	 }
	 
	 
	 public List<Musicas> getListaMusicas(String dir) throws Exception {

		 	String url = "http://lucianokikao.com.br/mobile/music.php?user=q1w2e3r4t5&pass=t5r4e3w2q1&dir="+dir;
			String[] resposta = new WebServiceApp().get(url);
			

			if (resposta[0].equals("200")) {
				Gson gson = new Gson();
				List<Musicas> musicas = new ArrayList<Musicas>();
				JsonParser parser = new JsonParser();

				 try{
		        	 JsonElement element = parser.parse(resposta[1]);
		        	 JsonArray array = element.getAsJsonArray();
		        	 for (int i = 0; i < array.size(); i++) {
		 	            musicas.add(gson.fromJson(array.get(i), Musicas.class));
		 	         }
		         }catch(Exception e){
		        	 throw new Exception("Falha ao acessar Internet");
		         }
				return musicas;
			}else{
				throw new Exception(resposta[1]);
			}
	 }
	 
	 public List<Musicas> getListaMusicasColetania(String dir) throws Exception {

		 	String url = "http://lucianokikao.com.br/mobile/faixas.php?user=q1w2e3r4t5&pass=t5r4e3w2q1&dir="+dir;
			String[] resposta = new WebServiceApp().get(url);
			

			if (resposta[0].equals("200")) {
				Gson gson = new Gson();
				List<Musicas> musicas = new ArrayList<Musicas>();
				JsonParser parser = new JsonParser();

				 try{
		        	 JsonElement element = parser.parse(resposta[1]);
		        	 JsonArray array = element.getAsJsonArray();
		        	 for (int i = 0; i < array.size(); i++) {
		 	            musicas.add(gson.fromJson(array.get(i), Musicas.class));
		 	         }
		         }catch(Exception e){
		        	 throw new Exception("Falha ao acessar Internet");
		         }
				return musicas;
			}else{
				throw new Exception(resposta[1]);
			}
	 }	 
	 
}
